<?php
class IndexController extends CommonController {
	public function index() {
		$this->assign ( 'car_list', M ( 'Car' )->order ( 'sort asc' )->select () );
		$this->assign ( 'model_list', json_encode ( D ( 'Model' )->get_model_list () ) );
		$this->assign ( 'displacement_list', json_encode ( D ( 'Displacement' )->get_displacement_list () ) );
		$this->display ();
	}
	public function save() {
		if ($this->isGet ()) {
			$displacement_id = intval ( $_GET ['displacement_id'] );
			$info = D ( 'Displacement' )->where ( "id={$displacement_id}" )->find ();
			$info ['car'] = M ( 'Car' )->where ( "id={$info['car_id']}" )->find ();
			$info ['model'] = M ( 'Model' )->where ( "id={$info['model_id']}" )->find ();
			$this->assign ( 'info', $info );
			$this->display ();
		} else {
			$db = D ( 'Appointment' );
			if (! $db->create ()) {
				$rs ['status'] = 2;
				$rs ['msg'] = $db->getError ();
			}
			if ($db->save ( $_POST )) {
				$rs ['status'] = 1;
			} else {
				$rs ['status'] = 3;
				$rs ['msg'] = '预约失败';
			}
			echo json_encode ( $rs );
		}
	}
// 	public function test() {
// 		set_time_limit(0);
// 		require CLASS_PATH.'/phpQuery.php';
// 		phpQuery::newDocumentFile('http://www.bopai.com/web/mobile/');
// 		$car_list=array();
// 		foreach(pq('#fm select option') as $v){
// 			$car_list[]=array('value'=>pq($v)->attr("value"),'text'=>pq($v)->text());
// 		}
// 		unset($car_list['0'],$car_list['51'],$car_list['52']);
// 		$model_list=array();
// 		foreach($car_list as $k=>$v){
// 			$data['z_data']='{"parent_id":"'.$v['value'].'"}';
// 			$url='http://www.bopai.com/web/mobile/?z_access_mode=web_service&ws_func=getCarData&session_page_id=1250567422&has_req_data=true';
// 			$content=curl_post($url,$data);
// 			$content=json_decode($content,true);
// 			phpQuery::newDocumentHTML($content['WS_RET_DATA']);
// 			$j=-1;
// 			foreach(pq('option') as $model){
// 				$value=pq($model)->attr("value");
// 				$text=pq($model)->text();
// 				$car_list[$k]['child'][++$j]=array('value'=>$value,'text'=>$text);
// 				$data['z_data']='{"parent_id":"'.$value.'"}';
// 				$content=curl_post($url,$data);
// 				$content=json_decode($content,true);
// 				phpQuery::newDocumentHTML($content['WS_RET_DATA']);
// 				foreach(pq('option') as $item){
// 					$car_list[$k]['child'][$j]['child'][]=array('value'=>pq($item)->attr("value"),'text'=>pq($item)->text());
// 				}
// 			}
// 		}
// 		dump($car_list);
// 		file_put_contents(ROOT_PATH.'/1.txt',var_export($car_list,true));
// 	}
// 	public function add(){
// 		$data=require ROOT_PATH.'/1.txt';
// 		foreach($data as $car){
// 			$carVO['name']=$car['text'];
// 			$carVO['sort']=50;
// 			$carVO['thumbnail']='';
// 			$car_id=M('Car')->save($carVO);
// 			foreach($car['child'] as $model){
// 				$modelVO['name']=$model['text'];
// 				$modelVO['sort']=50;
// 				$modelVO['car_id']=$car_id;
// 				$model_id=M('Model')->save($modelVO);
// 				foreach($model['child'] as $displacement){
// 					$displacementVO['name']=$displacement['text'];
// 					$displacementVO['car_id']=$car_id;
// 					$displacementVO['model_id']=$model_id;
// 					$displacementVO['sort']=50;
// 					M('Displacement')->save($displacementVO);
// 				}
// 			}
// 		}
// 	}
	public function project(){
		$data=M('Displacement')->select();
		foreach($data as $item){
			if(strstr($item['name'],'1.4L')!=false||strstr($item['name'],'1.6L')!=false||strstr($item['name'],'1.8L')!=false||strstr($item['name'],'1.4T')!=false||strstr($item['name'],'2.0L')!=false){
				$_data['displacement_id']=$item['id'];
				$_data['price']=230;
				$_data['project_id']=4;
				M('DisplacementProject')->save($_data);
				$_data['price']+=80;
				$_data['project_id']=5;
				M('DisplacementProject')->save($_data);
			}else if(strstr($item['name'],'1.6T')!=false||strstr($item['name'],'1.8T')!=false||strstr($item['name'],'2.0L')!=false||strstr($item['name'],'2.5')!=false||strstr($item['name'],'2.0T')!=false){
				$_data['displacement_id']=$item['id'];
				$_data['price']=420;
				$_data['project_id']=7;
				M('DisplacementProject')->save($_data);
				$_data['price']+=80;
				$_data['project_id']=9;
				M('DisplacementProject')->save($_data);
			}else if(strstr($item['name'],'2.8')!=false||strstr($item['name'],'3.0')!=false||strstr($item['name'],'3.5')!=false){
				$_data['displacement_id']=$item['id'];
				$_data['price']=550;
				$_data['project_id']=8;
				M('DisplacementProject')->save($_data);
				$_data['price']+=80;
				$_data['project_id']=10;
				M('DisplacementProject')->save($_data);
			}else{
				$_data['displacement_id']=$item['id'];
				$_data['price']=230;
				$_data['project_id']=4;
				M('DisplacementProject')->save($_data);
				$_data['price']+=80;
				$_data['project_id']=5;
				M('DisplacementProject')->save($_data);
			}
			$_data['price']=70;
			$_data['project_id']=6;
			M('DisplacementProject')->save($_data);
		}
	}
}